package com.example.subject10mvn.service.impl;

import com.example.subject10mvn.entity.Subject;
import com.example.subject10mvn.responsitory.SubjectResponsitory;
import com.example.subject10mvn.service.SubjectService;
import com.example.subject10mvn.vo.PageAndSubjects;
import com.example.subject10mvn.vo.Pagination;
import com.example.subject10mvn.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;


import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    SubjectResponsitory subjectResponsitory;

    public List<Subject> findAllWithPageAndSubjectTypeId(Pagination pagination, Integer subjectTypeId){
        //取出分页
        Pageable pageable= PageRequest.of(pagination.getPage(),pagination.getLimit());
        //判断subjectTypeId==0，如果为0查找所有数据，否则查找指定subjectTypeId的数据
        Page<Subject> subjectPage=subjectTypeId==0?subjectResponsitory.findAll (pageable):subjectResponsitory.findBySubjectTypeId(pageable,subjectTypeId);

        System.out.println(subjectPage.getContent());

        return subjectPage.getContent();
    }

    public String getFindAllDataTotal(Pagination pagination, Integer subjectTypeId){
        //取出分页
        Pageable pageable= PageRequest.of(pagination.getPage(),pagination.getLimit());

        Page<Subject> subjectPage=subjectTypeId==0?subjectResponsitory.findAll (pageable):subjectResponsitory.findBySubjectTypeId(pageable,subjectTypeId);

        return String.valueOf(subjectPage.getTotalElements());
    }

    public Subject getOne(Integer id){
        return subjectResponsitory.getOne(id);
    }

    public void insertOne(Subject subject){
        subjectResponsitory.saveAndFlush(subject);
    }

    public void deleteOne(Integer id){
        subjectResponsitory.deleteById(id);
    }

    public void updateOne(Subject subject){
        subjectResponsitory.save(subject);
    }

}
